package entities

import "errors"

var(
	BookNotFoundException = errors.New("no such book found")
)

type Student struct {
	Name  string
	Grade string
	Id    string
	Sex   string
	books []*BookItem
}

type BookItem struct {
	books Book
	count int
}

func CreateStudent(name, grade, id, sex string) (student *Student) {
	student = &Student{
		Name:  name,
		Grade: grade,
		Id:    id,
		Sex:   sex,
	}
	return

}

func (student *Student) AddBooks(books *BookItem) {

	student.books = append(student.books, books)
}

func (student *Student) DelBook(books BookItem)(err error){
	for i:=0;i<len(student.books);i++ {
		if student.books[i].books.Name == books.books.Name{
			//全部还完
			if books.count == student.books[i].count{
				front := student.books[0:i]
				hinder := student.books[i+1:]
				student.books = append(front, hinder...)
				return
			}
			//还部分
			student.books[i].count -= books.count
			return
		}
	}
	err = BookNotFoundException
	return
}
